﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ATM
{
    // 定义取款事件参数类
    public class BigMoneyArgs : EventArgs
    {
        public Account Account { get; set; }
        public decimal Amount { get; set; }

        public BigMoneyArgs(Account account, decimal amount)
        {
            Account = account;
            Amount = amount;
        }
    }

    // 定义自定义异常类
    public class BadCashException : Exception
    {
        public BadCashException(string message) : base(message)
        {
        }
    }

    // 账户基类
    public class Account
    {
        public string AccountNumber { get; set; }
        public decimal Balance { get; protected set; }

        public Account(string accountNumber, decimal initialBalance)
        {
            AccountNumber = accountNumber;
            Balance = initialBalance;
        }

        public virtual void Deposit(decimal amount)
        {
            Balance += amount;
            Console.WriteLine($"存款成功，当前余额：{Balance:C}");
        }

        public virtual void Withdraw(decimal amount)
        {
            if (amount > Balance)
            {
                Console.WriteLine("余额不足");
            }
            else
            {
                Balance -= amount;
                Console.WriteLine($"取款成功，当前余额：{Balance:C}");
            }
        }
    }

    // 信用账号类，继承自Account
    public class CreditAccount : Account
    {
        public decimal CreditLimit { get; set; }

        public CreditAccount(string accountNumber, decimal initialBalance, decimal creditLimit)
            : base(accountNumber, initialBalance)
        {
            CreditLimit = creditLimit;
        }

        public override void Withdraw(decimal amount)
        {
            if (amount > Balance + CreditLimit)
            {
                Console.WriteLine("超过信用额度，无法取款");
            }
            else
            {
                Balance -= amount;
                Console.WriteLine($"取款成功，当前余额：{Balance:C}");
            }
        }
    }

    // ATM类
    public class ATM
    {
        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        public void PerformTransaction(Account account, decimal amount)
        {
            Random random = new Random();
            if (random.Next(1, 10) <= 3) // 模拟30%的坏钞率
            {
                throw new BadCashException("发现破损钞票！");
            }

            if (amount > 10000)
            {
                OnBigMoneyFetched(new BigMoneyArgs(account, amount));
            }

            account.Withdraw(amount);
        }

        protected virtual void OnBigMoneyFetched(BigMoneyArgs e)
        {
            if (BigMoneyFetched != null)
            {
                BigMoneyFetched(this, e);
            }
        }
    }

    // 定义枚举类型
    public enum TransactionType
    {
        Deposit,
        Withdrawal
    }

    // 测试程序
    class Program
    {
        static void Main(string[] args)
        {
            ATM atm = new ATM();
            atm.BigMoneyFetched += Atm_BigMoneyFetched;

            // 创建账户
            Account normalAccount = new Account("Account", 0);
            CreditAccount creditAccount = new CreditAccount("CreditAccoumt", 0, 5000);

            // 控制台交互
            bool exit = false;
            while (!exit)
            {
                Console.WriteLine("\n请选择账号：");
                Console.WriteLine("1. 普通账号");
                Console.WriteLine("2. 信用账号");
                Console.WriteLine("3. 退出");
                string accountChoice = Console.ReadLine();

                Account selectedAccount = null;

                switch (accountChoice)
                {
                    case "1":
                        selectedAccount = normalAccount;
                        break;
                    case "2":
                        selectedAccount = creditAccount;
                        break;
                    case "3":
                        exit = true;
                        continue;
                    default:
                        Console.WriteLine("无效选择，请重试。");
                        continue;
                }

                Console.WriteLine("请选择操作：");
                Console.WriteLine("1. 存款");
                Console.WriteLine("2. 取款");
                Console.WriteLine("3. 返回上级菜单");
                string actionChoice = Console.ReadLine();

                switch (actionChoice)
                {
                    case "1":
                        Console.Write("请输入存款金额：");
                        decimal depositAmount;
                        if (decimal.TryParse(Console.ReadLine(), out depositAmount) && depositAmount > 0)
                        {
                            selectedAccount.Deposit(depositAmount);
                        }
                        else
                        {
                            Console.WriteLine("无效金额，请重试。");
                        }
                        break;

                    case "2":
                        Console.Write("请输入取款金额：");
                        decimal withdrawalAmount;
                        if (decimal.TryParse(Console.ReadLine(), out withdrawalAmount) && withdrawalAmount > 0)
                        {
                            try
                            {
                                atm.PerformTransaction(selectedAccount, withdrawalAmount);
                            }
                            catch (BadCashException ex)
                            {
                                Console.WriteLine($"错误：{ex.Message}");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效金额，请重试。");
                        }
                        break;

                    case "3":
                        continue;

                    default:
                        Console.WriteLine("无效选择，请重试。");
                        break;
                }
            }

            Console.WriteLine("程序结束。");
        }

        private static void Atm_BigMoneyFetched(object sender, BigMoneyArgs e)
        {
            Console.WriteLine($"警告：账号 {e.Account.AccountNumber} 取款 {e.Amount:C}，请注意！");
        }
    }

}
